package com.cc.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import com.cc.base.PageResponse;
import com.cc.base.ResponseResult;
import com.cc.enums.HttpsCode;
import com.cc.exception.BusinessException;
import com.cc.pojo.domain.Menu;
import com.cc.pojo.dto.MenuDto;
import com.cc.pojo.dto.MenuPageDto;
import com.cc.pojo.vo.MenuOptionsVo;
import com.cc.pojo.vo.MenuVo;
import com.cc.pojo.vo.OptionsVo;
import com.cc.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/menu/")
@Slf4j
@Tag(name = "菜单管理")
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     * 添加菜单
     *
     * @return
     */
    @PostMapping
    @Operation(summary = "添加菜单")
    public ResponseResult<String> addMenu(@RequestBody MenuDto menuDto) {
        return menuService.addMenu(menuDto);
    }

    /**
     * 删除菜单
     *
     * @return
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除菜单")
    public ResponseResult<String> removeMenu(@PathVariable("ids") List<Long> ids) {
        //1.检查参数
        if (CollectionUtil.isEmpty(ids)) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR, "参数为空");
        }
        boolean removeById = menuService.removeByIds(ids);
        if (!removeById) {
            throw new BusinessException(HttpsCode.REMOVE_ERROR);
        }
        return ResponseResult.ok();
    }

    /**
     * 修改菜单
     *
     * @return
     */
    @PutMapping
    @Operation(summary = "修改菜单")
    public ResponseResult<String> updateMenu(@RequestBody MenuDto menuDto) {
        return menuService.updateMenu(menuDto);
    }

    /**
     * 根据id查询菜单信息
     *
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询菜单信息")
    public ResponseResult<Menu> getMenuById(@PathVariable Long id) {
        //1.检查参数
        if (id == null) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR, "参数为空");
        }
        Menu menu = menuService.getById(id);
        return ResponseResult.ok(menu);
    }


    /**
     * 分页查询菜单
     *
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询菜单")
    public ResponseResult<PageResponse<MenuVo>> getMenuPage(@RequestBody MenuPageDto menuPageDto) {
        return menuService.getMenuPage(menuPageDto);
    }

    /**
     * 获取所有目录级别
     * 返回格式
     * [  { value: 1, label: '菜单1' },]
     *
     * @return
     */
    @GetMapping("/tableOfContents")
    @Operation(summary = "获取所有目录级别")
    public ResponseResult<List<OptionsVo>> tableOfContents() {
        return menuService.tableOfContents();
    }

    /**
     * 获取所有菜单列表 返回格式
     * @return
     */
    @GetMapping("/list/options")
    @Operation(summary = "获取菜单列表")
    public ResponseResult<List<MenuOptionsVo>> allListOptions() {
        return menuService.allListOptions();
    }
}